<template>
  <div>
    <div style="margin-bottom: 20px; font-size: 22px; font-weight: bold">评论 {{ commentCount }}</div>
    <div style="margin-bottom: 20px">
      <el-input type="textarea" placeholder="请输入评论" v-model="content"/>
      <div style="text-align: right; margin-top: 5px">
        <el-button type="primary" @click="addComment(null)">评论</el-button>
      </div>
    </div>

    <div>
      <div v-for="item in commentList" :key="item.id" style="margin-bottom: 20px">
        <!-- 一级评论-->
        <div style="display: flex; margin-bottom: 20px">
          <image-preview :src="item.avatar" :width="50" :height="50" style="border-radius: 50%"/>
          <div style="padding-left: 15px; flex: 1">
            <div style="margin-bottom: 10px; color: #666">{{ item.userName }}</div>
            <div style="margin-bottom: 5px">{{ item.content }}</div>
            <div style="color: #666; font-size: 13px; margin-bottom: 5px">
              <span>{{ item.time }}</span>
              <span @click="handleShowReply(item)" style="margin: 0 20px; cursor: pointer"
                    :class="{'comment-active' : item.showReply}"><i class="el-icon-s-comment"/>回复</span>
              <span @click="delComment(item.id)" v-if="item.userId === userId" style="cursor: pointer">
                <i class="el-icon-delete"/>删除</span>
            </div>
            <div v-if="item.showReply">
              <el-input type="textarea" placeholder="请输入回复" v-model="item.replyContent"/>
              <div style="text-align: right; margin-top: 5px">
                <el-button type="primary" @click="addComment(item)">回复</el-button>
              </div>
            </div>
          </div>
        </div>
        <!-- 二级评论-->
        <div v-if="item.children" style="padding-left: 65px">
          <div v-for="sub in item.children" style="display: flex; margin-bottom: 20px">
            <image-preview :src="sub.avatar" :width="50" :height="50" style="border-radius: 50%"/>
            <div style="padding-left: 15px; flex: 1">
              <div style="margin-bottom: 10px; color: #666">{{ sub.userName }} <span
                v-if="sub.parentUserName !== item.userName">回复：{{ sub.parentUserName }}</span></div>
              <div style="margin-bottom: 5px">{{ sub.content }}</div>
              <div style="color: #666; font-size: 13px; margin-bottom: 5px">
                <span>{{ sub.time }}</span>
                <span @click="handleShowReply(sub)" style="margin: 0 20px; cursor: pointer"
                      :class="{'comment-active' : sub.showReply}"><i class="el-icon-s-comment"/>回复</span>
                <span @click="delComment(sub.id)" v-if="sub.userId === userId" style="cursor: pointer"><i
                  class="el-icon-delete"/>删除</span>
              </div>
              <div v-if="sub.showReply">
                <el-input type="textarea" placeholder="请输入回复" v-model="sub.replyContent"/>
                <div style="text-align: right; margin-top: 5px">
                  <el-button type="primary" @click="addComment(sub)">回复</el-button>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import user from "@/store/modules/user";
import {addComment, delComment, selectCount, selectTree} from "@/api/project/comment";

export default {
  name: "CommentComponent",
  computed: {
    userId() {
      return user.state.id
    }
  },
  props: {
    fid: null,
    module: null
  },
  data() {
    return {
      commentCount: 0,
      content: '',
      commentList: []
    }
  },
  created() {
    this.loadComment()
  },
  methods: {
    handleShowReply(comment) {
      this.$set(comment, 'showReply', !comment.showReply)
    },
    delComment(commentId) {   // 单个删除
      this.$confirm('您确定删除吗？', '确认删除', {type: "warning"}).then(response => {
        delComment(commentId).then(res => {
          this.loadComment()
        })
      }).catch(() => {
      })
    },
    loadComment() {
      let params = {fid: this.fid, module: this.module}
      selectTree(params).then(res => {
        this.commentList = res.data || []
      })
      selectCount(params).then(res => {
        this.commentCount = res.data || 0
      })

    },
    addComment(comment) {  //  comment 有值表示这是回复
      let data = {fid: this.fid, userId: this.userId, module: this.module, content: this.content}
      if (comment) {
        data.content = comment.replyContent
        data.pid = comment.id
      }
      addComment(data).then(res=>{
        this.$message.success('操作成功')
        this.content = ''
        this.loadComment()
      })
    },
  }
}
</script>

<style scoped>

</style>
